<template>
  <basic-container>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-setting"></i> 账单管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <i class="el-icon-date"></i> 修改
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <avue-form ref="form" v-model="form" :option="modifyOption">
      <template slot-scope="scope" slot="menuForm">
        <el-button type="primary" @click="handleModify">提 交</el-button>
        <el-button @click="goBack">返 回</el-button>
      </template>
    </avue-form>
    <avue-detail ref="detail_form" v-model="detailForm" :option="option"></avue-detail>
    <avue-crud ref="crud" :option="orderDetailOption" :data="orderDetailList"></avue-crud>
  </basic-container>
</template>

<script>
import { amountModify, fetchInfo } from "../../api/bill/receivable";
import { formOption } from "../../const/crud/bill/form_option/receivable";
import { formOption as modifyOption } from "../../const/crud/bill/form_option/receivable_modify";
import { formOption as orderDetailOption } from "../../const/crud/bill/form_option/order_detail";
import { goBack } from "../../util/util";

export default {
  data() {
    return {
      form: {},
      detailForm: {},
      option: formOption,
      modifyOption: modifyOption,
      orderDetailOption: orderDetailOption,
      orderDetailList: []
    };
  },
  computed: {},
  created() {},
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.form.billSn = vm.$route.query.res.billSn;
      vm.queryData(vm.$route.query.res.billSn);
    });
  },
  methods: {
    queryData(billSn) {
      fetchInfo({ billSn: billSn })
        .then(response => {
          this.detailForm = Object.assign(
            {},
            response.data.data,
            response.data.data.orderInfo
          );
          this.orderDetailList = this.detailForm.detailList;
          this.form.billAmount = this.detailForm.billAmount;
          this.form.remarks = this.detailForm.remarks;
        })
        .catch(() => {
          this.goBack();
        });
    },
    handleModify() {
      let params = {
        billSn: this.form.billSn,
        billAmount: this.form.billAmount,
        remarks: this.form.remarks
      };
      amountModify(Object.assign({}, params)).then(response => {
        if (response.data.code === 0) {
          this.$message.success("修改成功");
          this.goBack();
        } else {
          this.$message.success(response.data.msg);
        }
      });
    },
    goBack(time = 1000) {
      goBack(this, time);
    }
  }
};
</script>

<style scoped lang="scss">
.crumbs {
  margin: 20px 10px;
}
</style>


